package com.bkq.cb;

import java.util.Calendar;

import com.bkq.cb.BLO.TransactionBLO;
import com.bkq.cb.DTO.TransactionDTO;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class SpendScreen extends Activity{
	TransactionDTO transactionObject = new TransactionDTO();
	TransactionBLO transactionBlo = new TransactionBLO(this);
	Button backBtn, saveBtn, cancelBtn;
	EditText nameTxt, dayTxt, moneyTxt;
	String idUser;
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.spendscreen);
	     backBtn = (Button)findViewById(R.id.backIma_spendScreen);
	     saveBtn = (Button)findViewById(R.id.saveBnt_spendScreen);
	     cancelBtn = (Button)findViewById(R.id.cancelBnt_spendScreen);
	     nameTxt = (EditText)findViewById(R.id.nameTxt_spendScreen);
	     dayTxt = (EditText)findViewById(R.id.dayTxt_spendScreen);
	     moneyTxt = (EditText)findViewById(R.id.moneyTxt_spendScreen);
	     dayTxt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// TODO Auto-generated method stub
				 int y,m,d;
					Calendar c = Calendar.getInstance();        
			            y = c.get(Calendar.YEAR);        
			              m= c.get(Calendar.MONTH);        
			             d = c.get(Calendar.DAY_OF_MONTH); 
			             DatePickerDialog dp = new DatePickerDialog(SpendScreen.this,new DatePickerDialog.OnDateSetListener() {
			            	   @Override
			                   public void onDateSet(DatePicker view, int year,
			                           int monthOfYear, int dayOfMonth) {
			                      String erg = null;
			                        erg = String.valueOf(dayOfMonth);                      
			                        erg += "/" + String.valueOf(monthOfYear+1);                    
			                        erg += "/" + year;  
			                    
			                       dayTxt.setText(erg);
			                   
			                   }
			            	   
			                 }, y, m, d);
			             
			              dp.setTitle("Choose Date");
			              dp.show();
			}
		});
	     Bundle extras = getIntent().getExtras();
		    if (extras != null) 
		    	idUser = extras.getString("idUser").toString();
	     
	     backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
	     
	     saveBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					transactionObject.IdUser = Integer.parseInt(idUser);
					transactionObject.Name = nameTxt.getText().toString();
					transactionObject.Date = dayTxt.getText().toString();
					transactionObject.Money = moneyTxt.getText().toString();
					transactionObject.Type = "spend";
					if(transactionBlo.insert(transactionObject))
					{
						nameTxt.setText("");
						dayTxt.setText("");
						moneyTxt.setText("");
						Toast.makeText(getBaseContext(), "Success"  , Toast.LENGTH_SHORT).show();
					}
					else 
						Toast.makeText(getBaseContext(), "Insert Error", Toast.LENGTH_SHORT).show();
					
				}
			});
	     
	     cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
	}
}
